package com.ucw.ucwclient.biz.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ucw.ucwclient.dal.dataobject.UCityCmt;
import com.ucw.ucwclient.dal.dataobject.UCityReply;
import com.ucw.ucwclient.dal.mapper.UCityCmtMapper;
import com.ucw.ucwclient.dal.mapper.UCityReplyMapper;
import com.ucw.ucwclient.dal.search.NormalSearch;

@Service
public class CmtService {

	@Resource
	private UCityCmtMapper cmtMapper;

	@Resource
	private UCityReplyMapper replyMapper;

	public List<UCityCmt> getCmtList(NormalSearch search) {
		List<UCityCmt> list = cmtMapper.selectByPageSearch(search);
		if (list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				List<UCityReply> replys = replyMapper.selectByCmtID(list.get(i).getId(), 3);
				if (replys.size() > 0) {
					list.get(i).setCityReply(replys);
				}
			}
			return list;
		}
		return null;
	}

	public List<UCityReply> getMoreReply(NormalSearch search) {
		return replyMapper.selectByPageSearch(search);
	}

	public Map<String, String> addCmt(UCityCmt cmt, Long userId) {
		if (cmt.getCmtContent() == null) {
			Map<String, String> resp = new HashMap<String, String>();
			resp.put("resultMsg", "noContent");
			return resp;
		}
		cmt.setUserId(userId);
		cmt.setCreateTime(new Date());
		int result = cmtMapper.insertSelective(cmt);
		Map<String, String> resp = new HashMap<String, String>();
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");

		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> addReply(UCityReply reply, Long userId) {
		if (reply.getReplyContent() == null) {
			Map<String, String> resp = new HashMap<String, String>();
			resp.put("resultMsg", "noContent");
			return resp;
		}
		reply.setUserId(userId);
		reply.setCreateTime(new Date());
		int result = replyMapper.insertSelective(reply);
		Map<String, String> resp = new HashMap<String, String>();
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");

		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> deleCmt(Integer id, Long userId) {
		int result = cmtMapper.deleteCmt(id, userId);
		Map<String, String> resp = new HashMap<String, String>();
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");

		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}

	public Map<String, String> deleReply(Integer id, Long userId) {
		int result = replyMapper.deleteReply(id, userId);
		Map<String, String> resp = new HashMap<String, String>();
		if (result == 1) {
			resp.put("resultMsg", "success");
			resp.put("code", "200");

		} else {
			resp.put("resultMsg", "fail");
		}
		return resp;
	}
}
